﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace ArgusLib.BugReport.MiniDumper
{
	class Program
	{
		/// <param name="args">
		/// args[0]: ProcessHandel (IntPtr)
		/// args[1]: ProcessID (int)
		/// args[2]: Filename (string)
		/// args[3]: MinidumpType (int)
		/// </param>
		static int Main(string[] args)
		{
			if (args == null || args.Length < 4)
				return -1;

			if (MiniDumpWriter.IsAvailable == false)
				return -1;

			long l;
			if (long.TryParse(args[0], out l) == false)
				return -1;
			IntPtr processHandel = new IntPtr(l);
			Console.WriteLine(string.Format("ProcessHandel: {0}", processHandel));
			int processId;
			if (int.TryParse(args[1], out processId) == false)
				return -1;
			Console.WriteLine(string.Format("ProcessID: {0}", processId));
			FileInfo fileInfo;
			try { fileInfo = new FileInfo(args[2]); }
			catch { return -1; }
			Console.WriteLine(string.Format("Filename: {0}", fileInfo.FullName));
			int mType;
			if (int.TryParse(args[3], out mType) == false)
				return -1;
			MinidumpType minidumpType = (MinidumpType)mType;
			Console.WriteLine(string.Format("MinidumpType: {0}", minidumpType));

			using (FileStream file = fileInfo.Open(FileMode.Create))
			{
				MiniDumpWriter.MiniDumpWriteDump(processHandel, processId, file.SafeFileHandle.DangerousGetHandle(),
					minidumpType, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
			}
			return 0;
		}
	}
}
